import Taddress from '@/components/threeAddress.vue'
export default {
  components: {
    Taddress,
  },
  data() {
    return {
      keyDown: false,
      active: '',
      tabList: [{
        name: '家',
        id: '1',
      },
      {
        name: '公司',
        id: '2'
      }
      ],
      showdz: false,
      taddress: [],
      tabs: '',
      checkedValue: '',
      area: 0,
      city: 0,
      province: 0,
      activetrue: false,//switch初始值
      labelValue: 0,//标签值
    }
  },
  created() {

  },
  onLoad(option) {
    let list = JSON.parse(option.list)
    this.taddress = list
    this.active = list.address_label
    this.area = this.taddress.district
    this.city = this.taddress.city
    this.province = this.taddress.province//省
    this.checkedValue = Number(list.isdefault) //是否默认（0false、不默认 1、true默认）
    if (this.checkedValue === 1) {
      this.activetrue = true
    } else {
      this.activetrue = false
    }
  },
  methods: {
    valChange(val) {
      console.log(val)
      // 将每次按键的值拼接到value变量中，注意+=写法
      if(this.taddress.mobile.length>=11) return false
      this.taddress.mobile += val
      if (this.taddress.mobile.trim().length !== 11) return false
      // console.log(this.mobile)
      if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.taddress.mobile))) {
        this.$u.toast('请输入正确的手机号')
      }
    },
    // 退格键被点击
    backspace() {
      // 删除value的最后一个字符
      if (this.taddress.mobile.length) this.taddress.mobile = this.taddress.mobile.substr(0, this.taddress.mobile.length - 1)
    },
    input(){
      if (this.taddress.mobile.trim().length !== 11) return false
      // console.log(this.mobile)
      if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.taddress.mobile))) {
			  this.$u.toast('请输入正确的手机号')
      }
    },
    change(status) {
      // console.log(status)
      this.checkedValue = status
      console.log(this.checkedValue,'111')
      // console.log(status)
    },
    tab(id) {
      // 单选
      if (id !== this.active) {
        this.active = id
      } else {
        this.active = -1
        id = '0'
      }
      console.log(id)
      this.active = id
    },
    confirmdz(e) {
      console.log(e)
      this.area = Number(e.area.value)
      this.city = e.city.id
      this.province = e.province.id
      this.taddress.mcdistrict = e.area.label
      this.taddress.mccity = e.city.label
      this.taddress.mcprovince = e.province.label
      this.showdz = false
    },
    close() {
      this.showdz = false
    },
    //编辑
    async keepData() {
      if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.taddress.mobile))){
        this.$u.toast('请输入正确的手机号')
        return false
      }
      let data = {
        'name': this.taddress.name,
        'mobile': this.taddress.mobile,
        'address': this.taddress.address,
        'province': this.province,
        'city': this.city,//市
        'district': this.area,//区
        'address_label': Number(this.active),
        'isdefault': this.checkedValue,
        'id': this.taddress.id,
        'app_type': 1
      }
      const res = await this.$api.common.editAddressList(data)
      this.$u.toast('修改成功')
      setTimeout(res => {
        uni.navigateBack({
          delta: 1
        })
      }, 1000)
    },
    //删除地址
    async delAddress() {
      try {
        let data = {
          'id': this.taddress.id
        }
        const res = await this.$api.common.delAddressList(data)
        this.$u.toast('删除成功')
        setTimeout(res => {
          uni.navigateBack({
            delta: 1
          })
        }, 1000)

      } catch (e) {
        console.log(e)
      }
    }
  }
}
